/*
 * Copyright (C) 2004, Velocityme Partnership
 * DepartmentActions.java
 *
 * Created on 05 October 2004, 05:28
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.DepartmentValueActionForm;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class DepartmentActions extends ContactableActions {
    
    /** Creates a new instance of DepartmentActions */
    public DepartmentActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        DepartmentPK departmentPK = new DepartmentPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("departmentValue", cs.getDepartmentValue(key, departmentPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        DepartmentPK departmentPK = new DepartmentPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        DepartmentValueActionForm departmentValueActionForm = new DepartmentValueActionForm();
        createContactDetails(key, cs, departmentValueActionForm);
        session.setAttribute("departmentActionForm", departmentValueActionForm);
        DepartmentValue departmentValue = new DepartmentValue();
        departmentValue.setContactableValue(new ContactableValue());
        departmentValue.getContactableValue().setNodeValue(new NodeValue());
        session.setAttribute("departmentValue", departmentValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        DepartmentPK departmentPK = new DepartmentPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        DepartmentValue departmentValue = cs.getDepartmentValue(key, departmentPK);
        session.setAttribute("departmentValue", departmentValue);
        DepartmentValueActionForm departmentValueActionForm = new DepartmentValueActionForm();
        departmentValueActionForm.setName(departmentValue.getContactableValue().getNodeValue().getName());
        departmentValueActionForm.setDescription(departmentValue.getContactableValue().getNodeValue().getDescription());
        editContactDetails(key, cs, departmentValue.getContactableValue(), departmentValueActionForm);
        session.setAttribute("departmentActionForm", departmentValueActionForm);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            DepartmentValue departmentValue = (DepartmentValue)session.getAttribute("departmentValue");
            DepartmentValueActionForm departmentValueActionForm = (DepartmentValueActionForm)form;
            departmentValue.getContactableValue().getNodeValue().setName(departmentValueActionForm.getName());
            departmentValue.getContactableValue().getNodeValue().setDescription(departmentValueActionForm.getDescription());
            departmentValue.getContactableValue().setContactDetailValues(departmentValueActionForm.getContactDetailValues(cs.getAllContactDetailValues(key)));
            DepartmentPK departmentPK;
            if (departmentValue.departmentIdHasBeenSet()) {
                departmentPK = departmentValue.getPrimaryKey();
                cs.departmentEdit(key, departmentPK, departmentValue);
            }
            else {
                ActionErrors errors = new ActionErrors();
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                departmentPK = cs.departmentCreate(key, parentNodePK, departmentValue);
            }
            nodePK = new NodePK(departmentPK.getDepartmentId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
